﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BG_Manage_BG_Friendlink : System.Web.UI.Page
{
    VPINews.BLL.FriendLink bll_fLink = new VPINews.BLL.FriendLink();
    VPINews.Model.FriendLink mod_fLink = new VPINews.Model.FriendLink();
    private static int linkid;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            getfLink();
        }
    }

    protected void getfLink()
    {
        GridView1.DataSource = bll_fLink.GetAllList();
        GridView1.DataBind();
    }


    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = bll_fLink.GetAllList();
        GridView1.DataBind();
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            int linkid = Convert.ToInt32(hdID.Value);
            
            if (linkid > 0)
            {
                mod_fLink.LinkID = linkid;
                mod_fLink.LinkName = this.txtName.Value.Trim();
                mod_fLink.LinkURL = this.txtUrl.Value.Trim();
              
                bll_fLink.Update(mod_fLink);
                smslabel.Text = "链接修改成功";

                txtName.Value = "";
                txtUrl.Value = "";
                hdID.Value = "0";
                lab.Text = "添加友情链接";
                btnSave.Text = "· 添加 ·";
            }

            else
            {
                string Name = this.txtName.Value;
                string Url = this.txtUrl.Value;

               
               
                if (bll_fLink.ExistsByName(Name))
                {
                    smslabel.Text = "添加失败,已存在相同的链接";
                    txtName.Value = "";
                    txtUrl.Value = "";
                }

                else
                {
                    mod_fLink.LinkName = Name;
                    mod_fLink.LinkURL = Url;
                    bll_fLink.Add(mod_fLink);
                    smslabel.Text = "链接添加成功";
                    getfLink();
                    txtName.Value = "";
                    txtUrl.Value = "";
                }

               
            }

        }
        getfLink();
    }

        /*
        if (Page.IsValid)
        {
            if (labshow.Text == "修改管理员信息" || (int)Session["userID"] == 1)
            {
                string u = (string)Session["User"];
                string UserName = this.txtUserName.Value;
                string PassWord = this.txtPWDOK.Value;
                mod_admin.UserName = UserName;
                mod_admin.PassWord = PassWord;

                if (UserName == u)
                {
                    smslabel.Text = "1号管理员的账号禁止修改";
                }


                else
                {
                    bll_admin.Update(mod_admin);
                    smslabel.Text = "管理员修改成功";
                    getAdmin();
                    txtUserName.Value = "";

                    labshow.Text = "添加管理员信息";
                    btnAdd.Text = "· 添加 ·";
                }

            }

            else
            {
                string UserName = this.txtUserName.Value;
                string PassWord = this.txtPWDOK.Value;

                mod_admin.UserName = UserName;
                mod_admin.PassWord = PassWord;

                if (bll_admin.Exists(UserName))
                {
                    smslabel.Text = "管理员已存在，添加失败";
                    txtUserName.Value = "";

                }

                else
                {
                    bll_admin.Add(mod_admin);
                    smslabel.Text = "管理员添加成功";
                    getAdmin();
                    txtUserName.Value = "";
                }
            }
        }
         * */

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        VPINews.BLL.FriendLink bll_fLink = new VPINews.BLL.FriendLink();
        VPINews.Model.FriendLink mod_fLink = bll_fLink.GetModel(Convert.ToInt32(e.CommandArgument));

        if (e.CommandName == "DeleteAdmin")
        {
            bll_fLink.Delete(Convert.ToInt32(e.CommandArgument));
            getfLink();
        }

        if (e.CommandName == "EditAdmin")
        {
            lab.Text = "修改友情链接";
            btnSave.Text = "· 修改 ·";
                 
            this.txtName.Value =mod_fLink.LinkName;
            this.txtUrl.Value = mod_fLink.LinkURL;
            linkid = mod_fLink.LinkID;
            this.hdID.Value = linkid.ToString();

        }
    }
}
